<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Models\User;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    public function all(Request $request)
    {
        $userId = $request->get('user');
        $user = User::find($userId);
        $query = Order::where('status', '>', 0);
        if (!empty($user)) {
            $query = $query->where('user_id', $user['id']);
        }
        $limit = $request->get('limit', config('common.pagination.limit'));
        $items = $query
            ->paginate($limit);
        return jsonResponse(config('common.successCode'), $items);
    }

    /**
     * 后台取消订单
     * @param Request $request
     * @param $id
     * @return array
     */
    public function cancel(Request $request, $id)
    {
        $order = Order::find($id);
        if (empty($order)) {
            return jsonResponse(40000);
        }
        if ($order['status'] == config('common.order.status.submit')) {
            $order['status'] = config('common.order.status.cancel');
            $order->save();
        } else {
            return jsonResponse(41400);
        }
        return jsonResponse();
    }
}
